<?php
global $_W, $_GPC;
$weid = $this->_weid;
$from_user = $this->_fromuser;
$setting = $this->getSetting();
$orderid = intval($_GPC['orderid']);

$do = 'changeuser';
if ($setting['auth_mode'] == 1 || empty($setting)) {
    $method = 'order'; //method
    $host = $this->getOAuthHost();
    $authurl = $host . 'app/' . $this->createMobileUrl($method, array('orderid' => $orderid), true) . '&authkey=1';
    $url = $host . 'app/' . $this->createMobileUrl($method, array('orderid' => $orderid), true);
    if (isset($_COOKIE[$this->_auth2_openid])) {
        $from_user = $_COOKIE[$this->_auth2_openid];
        $nickname = $_COOKIE[$this->_auth2_nickname];
        $headimgurl = $_COOKIE[$this->_auth2_headimgurl];
    } else {
        if (isset($_GPC['code'])) {
            $userinfo = $this->oauth2($authurl);
            if (!empty($userinfo)) {
                $from_user = $userinfo["openid"];
                $nickname = $userinfo["nickname"];
                $headimgurl = $userinfo["headimgurl"];
            } else {
                message('授权失败!');
            }
        } else {
            if (!empty($this->_appsecret)) {
                $this->getCode($url);
            }
        }
    }
} else {
    load()->model('mc');
    if (empty($_W['fans']['nickname'])) {
        mc_oauth_userinfo();
    }
    $from_user = $_W['fans']['openid'];
    $nickname = $_W['fans']['nickname'];
    $headimgurl = $_W['fans']['tag']['avatar'];
}


if (empty($from_user)) {
    message('会话已过期，请重新发送关键字!');
}

$order = pdo_fetch("SELECT * FROM " . tablename($this->table_order) . "  WHERE id =:id ORDER BY id DESC LIMIT 1", array(':id' => $orderid));

if (empty($order)) {
    message('订单不存在!');
}

if ($order['ispay'] == 1) {
    message('订单已经支付了!');
}

pdo_update($this->table_order, array('from_user' => $from_user), array('id' => $orderid));

//echo 'orderuser:' . $order['from_user'] . '////curuser' . $from_user;
//exit;

$url = $this->createMobileUrl('pay', array('orderid' => $orderid), true);
Header("Location: {$url}");